package com.cosina.game.robodefence.findway;



public class NeatWay {
	
	public static Path tranlate(int[][] way){
		Path dotResult = null;
		Path dot = null;
		for(int y=0; y<way.length; y++){
			for(int x=0; x<way[0].length; x++){
				if(way[y][x] == 1){
					dot = new Path(x,y,null);
					dotResult = dot;
				}else if(way[y][x] < 0){
					way[y][x] = 0;
				}
				
			}
		}
		int rawValue = 1;
		while((dot = getLagerDotAround(way, dot,rawValue)) != null){
			rawValue = way[dot.y][dot.x];
		}
		
		return dotResult;
	}
	
	public static Path getLagerDotAround(int[][] way, Path center,int rawValue){
		int resultX=-1, resultY=-1;
		
		if(center.x>0 && way[center.y][center.x-1] > rawValue){
			resultX = center.x-1;
			resultY = center.y;
			rawValue = way[center.y][center.x-1];
		}
		if(center.y>0 && way[center.y-1][center.x] > rawValue){
			resultX = center.x;
			resultY = center.y-1;
			rawValue = way[center.y-1][center.x];
		}
		if(center.y<(way.length-1) && way[center.y+1][center.x] > rawValue){
			resultX = center.x;
			resultY = center.y+1;
			rawValue = way[center.y+1][center.x];
		}
		if(center.x<(way[0].length-1) && way[center.y][center.x+1] > rawValue){
			resultX = center.x+1;
			resultY = center.y;
			rawValue = way[center.y][center.x+1];
		}
		if(resultX == -1)
			return null;
		else{
			return new Path(resultX,resultY, center);
		}
	}
}
